// #define print(...)

#define IS_DIGIT(x) ('0'<=(x) && (x)<='9')
#define IS_SIGN(x)  ('-'==(x) ? -1 : ((x)=='+' ? 1 : 0))
class Solution {
public:
    int myAtoi(string s) {
        int sign = 1;
        auto p = s.begin(), end=s.end();
        while(p!=end && ' ' == *p) ++p;
        if(p!=end && !IS_DIGIT(*p) && !IS_SIGN(*p)) return 0;
        if(p!=end && IS_SIGN(*p)){
            sign=IS_SIGN(*p);
            ++p;
        }
        int64_t ret = 0;
        while(p!=end && IS_DIGIT(*p)){
            ret = ret*10 + (*p)-'0';
            ++p;
            if(1==sign){
                ret = min(ret, (int64_t)INT_MAX);
            }else if(-1==sign){
                ret = min(ret, -(int64_t)INT_MIN);
            }
        }
        return int(sign * ret);
    }
};
